/*
# MIAHX© 用户感知数据帧消息的 FastDDS 发布器/订阅器（C++实现）

Version: 0.4.10,
Last updated: October 19, 2024

## Contributor(s)
* YAO Naiming <[naiming@iscas.ac.cn](mailto:naiming@iscas.ac.cn)>

## Change log
### v0.4.10
- .19: [新增] 初始实现

---
Copyright © 2024-2025 MIAHX Contributors, ISCAS, and ASIC. All Rights Reserved.
Licensed under Apache-2.0.
*/

#include "../include/miahx/user_reader.h"


class UserReaderListener : public ReaderListener {
public:
    explicit UserReaderListener(TopicDataType *topic_data_type, data_listener &on_data_listener,
                                void *user_state)
            : ReaderListener(topic_data_type, on_data_listener, user_state) {}

protected:
    void on_data_available(DataReader *reader) override {
        try {
            SampleInfo info;
            UserDataFrame frame;
            if (reader->take_next_sample(&frame, &info) == ReturnCode_t::RETCODE_OK) {
                if (info.valid_data) {
                    mOnDataListener(&frame);
                }
            }
        } catch (const exception &ex) {
            cout << ex.what() << endl;
        }
    }
};


UserStreamReader::UserStreamReader(Participant *participant,
                                   const string &topic_name,
                                   data_listener &on_data_listener,
                                   void *user_state)
        : Reader(participant, topic_name,
                 new UserDataFramePubSubType(),
                 new UserReaderListener(new UserDataFramePubSubType(),
                                        on_data_listener, user_state)) {}
